(function () {
    var app = angular.module("DIBS");
    app.controller("searchServiceInfoCtrl", ['$http', 'uiGridConstants', searchServiceInfoCtrl]);
    searchServiceInfoCtrl.$inject = ['$http'];
    function searchServiceInfoCtrl($http) {
        var vm = this;
        vm.updateServiceInfo = function(data,type) {
            $http({
                method: "POST",
                url: "/updateServiceInfo",
                data: {serviceInfo: data, type: type}
            }).success(function (result, status) {
               if(type === 'Delete'){
                   _.remove(vm.serviceInfoGrid.data, function(record){
                       return record._id == data._id;
                   });
                   setTimeout(vm.closeConfirmDeleteWindow(),2000);
               }
                if(type === 'Update'){
                    _.each(vm.serviceInfoGrid.data, function(rec){
                        if(rec._id === data._id){
                            rec.serviceType = data.serviceType;
                            rec.producer = data.producer;
                            rec.consumer = data.consumer;
                            rec.detail = data.detail;
                            rec.remark = data.remark;
                            return false;
                        }
                    });
                    $('#serviceInfoEditModel').modal('hide');
                }
            }).error(function (result, status) {
                //vm.data = data || "Request failed";
                //vm.status = status;
            });
        };

        vm.search = function () {
            $http({
                method: "POST",
                url: "searchServiceInfo",
                data: {serviceInfo: vm.serviceInfo}
            }).success(function (data, status) {
                vm.serviceInfoGrid.data = data;
                $('#addedUserModal').modal('show');
            }).error(function (data, status) {
                console.log("data");
                //vm.data = data || "Request failed";
                //vm.status = status;
            });
        };

        vm.serviceInfoGrid = {
            columnDefs: [{field: 'serviceType', displayName: 'Service Type', width: "10%"},
                {field: 'producer', displayName: 'Producer', width: "10%"},
                {field: 'consumer', displayName: 'Consumer', width: "10%"},
                {field: 'detail', displayName: 'Detail', width: "20%"},
                {field: 'remark', displayName: 'Remark', width: "25%"},
                {
                    name: 'operation',
                    displayName: 'Operation',
                    enableFiltering: false,
                    width: "25%",
                    cellTemplate: "<button id='editBtn' ng-disabled='grid.appScope.GLOBAL_ReadOnly' type='button' class='btn btn-warning btn-sm' ng-click='grid.appScope.vm.editServiceInfo(row.entity)' style='margin-left: 10px; width: 55px; height: 26px;margin-top: 3px' >Edit</button>" +
                    "<button id='deleteBtn' type='button' ng-disabled='grid.appScope.GLOBAL_ReadOnly' class='btn btn-danger btn-sm' ng-click='grid.appScope.vm.deleteServiceInfo(row.entity)' style='margin-left: 10px; width: 55px; height: 26px; margin-top: 3px' >Delete</button>"
                }
            ],
            data: [],
            cellEditableCondition: false,
            paginationPageSize: 20,
            paginationPageSizes: [20, 100, 500],
            enableFiltering: true,
            enableGroupHeaderSelection: true,
            treeRowHeaderAlwaysVisible: true,
            enableColumnResizing: true,
            multiSelect: true,
            enableRowHeaderSelection: true,
            enableRowSelection: true,

            enableGridMenu: true,
            enableSelectAll: true,
            exporterCsvFilename: 'HardwareReport.csv',
            exporterPdfDefaultStyle: {fontSize: 9},
            exporterPdfTableStyle: {margin: [30, 30, 30, 30]},
            exporterPdfTableHeaderStyle: {fontSize: 10, bold: true, italics: true, color: 'red'},
            exporterPdfHeader: {text: "My Header", style: 'headerStyle'},
            exporterPdfFooter: function (currentPage, pageCount) {
                return {text: currentPage.toString() + ' of ' + pageCount.toString(), style: 'footerStyle'};
            },
            exporterPdfCustomFormatter: function (docDefinition) {
                docDefinition.styles.headerStyle = {fontSize: 22, bold: true};
                docDefinition.styles.footerStyle = {fontSize: 10, bold: true};
                return docDefinition;
            },
            exporterPdfOrientation: 'portrait',
            exporterPdfPageSize: 'LETTER',
            exporterPdfMaxGridWidth: 500,
            exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
            onRegisterApi: function (gridApi) {
                vm.gridApi = gridApi;
            }
        };

        vm.editServiceInfo = function(ServiceInfo) {
            vm.saveResult = null;
            vm.type = 'update';
            $('#modalTitle').text('Edit Service Info');
            $('#saveBtn').text('Update');
            vm.serviceType = ServiceInfo.serviceType;
            vm.producer = ServiceInfo.producer;
            vm.consumer = ServiceInfo.consumer;
            vm.detail = ServiceInfo.detail;
            vm.remark = ServiceInfo.remark;
            $('#serviceInfoEditModel').modal('show');
            vm._id=ServiceInfo._id;
        };
        vm.confirmEditServiceInfo = function(){
            var updateObj = {};
            updateObj.serviceType = vm.serviceType;
            updateObj.producer = vm.producer;
            updateObj.consumer = vm.consumer;
            updateObj.detail = vm.detail;
            updateObj.remark = vm.remark;
            updateObj._id = vm._id;
            vm.updateServiceInfo(updateObj,'Update');
        };

        vm.deleteServiceInfo = function(serviceInfo) {
            vm.id = null;
            $('#deleteConfirm').modal('show');
            vm.id =serviceInfo._id;
        };

        vm.closeConfirmDeleteWindow = function(){
            $('#deleteConfirm').modal('hide');
        };

        vm.confirmDelete = function(){
            vm.updateServiceInfo({_id:vm.id},'Delete');
        };

        function removeGridData(data, removeId){
            _.each(data, function(record){

            })
        }
    }
})();